// SPDX-License-Identifier: GPL-2.0 or GPL-3.0
// Copyright © 2018-2019 Ariadne Devos
// sHT -- generic file descriptor operations (Unix)

#include "fd.h"
#include <sHT/bugs.h>
#include <sHT/compiler.h>
#include <sHT/test.h>

#include <errno.h>
#include <unistd.h>

static const struct sHT_bug_option sHT_close_bug =
  { .code = 0, .msg = "not a fd" };

void
sHT_close(int fd)
{
	/* close must not be retried on interrupt on:
	   Linux, FreeBSD, OpenBSD (man pages),
	   don't know about NetBSD,
	   I presume the Hurd. */
	int ret = close(fd);
	if (!sHT_nonzero_p(ret))
		return;
	int err = errno;
	if (sHT_eq(err, EBADFD))
		sHT_bug(&sHT_close_bug, err);
	return;
}
